package com.woshua.vptabmodel;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.woshua.vptabmodel.fragment.TabFragment;
import com.woshua.vptabmodel.tabbar.ItemBar;
import com.woshua.vptabmodel.utils.L;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// fragment触发更改Activity的数据  通过回调进行处理
public class MainActivity extends AppCompatActivity {

    private List<String> mTitles=new ArrayList<>(Arrays.asList("微信","通讯录","发现","我"));
    private int[] mImages={R.drawable.icon_home,R.drawable.icon_friend,R.drawable.icon_money,R.drawable.icon_me};
    private int[] mImageSels={R.drawable.icon_home_sel,R.drawable.icon_friend_sel,R.drawable.icon_money_sel,R.drawable.icon_me_sel};


    private SparseArray<Fragment> mFragments=new SparseArray<>();

    public static final String BUNDLE_POS_SEL="bundle_pos_sel";

    private ItemBar item_bar_home;
    private ItemBar item_bar_friend;
    private ItemBar item_bar_money;
    private ItemBar item_bar_me;

    private ArrayList<ItemBar> itemBars=new ArrayList<>();

    private ViewPager vpContent;

    private int selectPos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vpContent = findViewById(R.id.vpContent);

        if (savedInstanceState!=null)
        {
            selectPos=savedInstanceState.getInt(BUNDLE_POS_SEL,0);
        }

        initViewPager();

        initViews();

        initListener();

        initEvents();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(BUNDLE_POS_SEL,vpContent.getCurrentItem());
        super.onSaveInstanceState(outState);
    }

    private void initViewPager()
    {
        vpContent.setOffscreenPageLimit(4);
        vpContent.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return TabFragment.newInstance(mTitles.get(i));
            }

            @Override
            public int getCount() {
                return mTitles.size();
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                TabFragment tabFragment= (TabFragment) super.instantiateItem(container, position);
                mFragments.put(position,tabFragment);
                return tabFragment;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                mFragments.remove(position);
                super.destroyItem(container, position, object);
            }
        });
    }

    private void initViews()
    {
        item_bar_home=findViewById(R.id.item_bar_home);
        item_bar_friend=findViewById(R.id.item_bar_friend);
        item_bar_money=findViewById(R.id.item_bar_money);
        item_bar_me=findViewById(R.id.item_bar_me);
        itemBars.add(item_bar_home);
        itemBars.add(item_bar_friend);
        itemBars.add(item_bar_money);
        itemBars.add(item_bar_me);

        for (int i = 0; i < itemBars.size(); i++) {
            itemBars.get(i).setItemBar(mTitles.get(i),mImages[i],mImageSels[i]);
        }

        setCurrentTabPos(selectPos);
    }

    private void initListener() {
       vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int pos, float progress, int i1) {
               L.d("位置pos:%d,进度%f",pos,progress);

               //规律:
               //左->右  左边:0->1 left:pos 右边:0~1 right:pos+1
               //右边->左边 左边:1->0 left:pos 右边:1~0 right:pos+1

               if(progress>0) {
                   ItemBar leftBar = itemBars.get(pos);
                   ItemBar rightBar = itemBars.get(pos + 1);

                   leftBar.setProgress(1-progress);
                   rightBar.setProgress(progress);
               }
           }

           @Override
           public void onPageSelected(int i) {

           }

           @Override
           public void onPageScrollStateChanged(int i) {

           }
       });
    }

    private void initEvents() {
        for (int i = 0; i < itemBars.size(); i++) {
            final ItemBar itemBar = itemBars.get(i);
            final int finalI = i;
            itemBar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vpContent.setCurrentItem(finalI,false);
                    setCurrentTabPos(finalI);
                    //Activity 调用fragment更改数据
//                    final TabFragment fragment = (TabFragment) mFragments.get(finalI);
//                    if(fragment!=null)
//                    {
//                        fragment.changeTitle(""+finalI);
//                    }
                }
            });
        }
    }

    private void setCurrentTabPos(int pos)
    {
        for (int i = 0; i < itemBars.size(); i++) {
            final ItemBar itemBar = itemBars.get(i);
            if(i==pos)
            {
                itemBar.setProgress(1);
            }
            else
            {
                itemBar.setProgress(0);
            }
        }
    }
}
